﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// Function 的摘要说明
/// </summary>
public class Function
{
    int k = 0;
    string str = "";

    //判断管理员是否登录
    public static void CheckSession()
    {
        if (System.Web.HttpContext.Current.Session["adminname"] == null)
        {
            System.Web.HttpContext.Current.Response.Write("<script>alert('登录超时...');location.href('/index.aspx');</script>");
        }
    }


    public string GetTree(int id)
    {
        k = k + 1;
        SqlDataReader reader = SqlHelper.ExecuteReader("select * from class where parentid=" + id);
        while (reader.Read())
        {
            int cid = reader.GetInt32(0);
            string classname = reader.GetString(2);
            str = str + "<option value=" + cid + ">";
            for (int i = 1; i <= k; i++)
            {
                if (i != 1)
                {
                    str = str + "├&nbsp;";
                }
            }
            str = str + classname + "</option>";
            GetTree(cid);
            k = k - 1;
        }
        reader.Close();
        reader.Dispose();

        return str;
    }
    

    public string UpdateClass(int id, int curid)
    {
        k = k + 1;
        SqlDataReader reader = SqlHelper.ExecuteReader("select * from class where parentid=" + id);
        while (reader.Read())
        {
            int cid = reader.GetInt32(0);
            string classname = reader.GetString(2);
            str = str + "<option value=" + cid + ">";
            for (int i = 1; i <= k; i++)
            {
                if (i != 1)
                {
                    str = str + "├&nbsp;";
                }
            }
            str = str + classname + "</option>";
            UpdateClass(cid, curid);
            k = k - 1;
        }
        reader.Close();
        reader.Dispose();

        return str;
    }


    public string ShowClass(int type)
    {
        if (type == 0)
        {
            System.Web.HttpContext.Current.Response.Write("<option value=0>无<作为一级栏目></option>");
        }
        return GetTree(0);

    }

    public string ManageClass(int id)
    {
        k = k + 1;
        SqlDataReader reader = SqlHelper.ExecuteReader("select * from class where parentid=" + id);
        while (reader.Read())
        {
            int cid = reader.GetInt32(0);
            string classname = reader.GetString(2);
            str = str + "<div align='left'>";
            for (int i = 1; i <= k; i++)
            {
                if (i != 1)
                {
                    str = str + "├&nbsp;";
                }
            }
            str = str + classname + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span align='right'><a href='delclass.aspx?cid=" + cid + "'>删除</a></span></div>";
            ManageClass(cid);
            k = k - 1;
        }
        reader.Close();
        reader.Dispose();
        return str;
    }

    public static string GetSubstring(string str, int length)
    {
        if (str.Length > length)
        {
            return str.Substring(0, length - 1) + "...";
        }
        else
        {
            return str;
        }
    }

    public static string FormatDatetime(DateTime time)
    {
        return time.Year + "-" + time.Month + "-" + time.Day;
    }

    public static string ReturnMd5Pwd(string pwd)
    {
        return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pwd.Trim(), "md5");
    }


    public static void Main()
    {
        string s = StripHTML("<HTML><HEAD><TITLE>宜昌三木网络技术有限公司</TITLE></HEAD><BODY>宜昌三木信息平台</BODY></HTML>");
        Console.WriteLine(s);
    }

    public static string GetClassNameById(int classid)
    {
        DataTable dt = new DataTable();
        Class c=new Class();
        string classname;
        dt = c.GetClassById(classid);
        classname = dt.Rows[0]["classname"].ToString();
        return classname;
    }

    public static string StripHTML(string strHtml)
    {

        string[] aryReg ={   
                      @"<script[^>]*?>.*?</script>",   
    
                      @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",   
                      @"([\r\n])[\s]+",   
                      @"&(quot|#34);",   
                      @"&(amp|#38);",   
                      @"&(lt|#60);",   
                      @"&(gt|#62);",     
                      @"&(nbsp|#160);",     
                      @"&(iexcl|#161);",   
                      @"&(cent|#162);",   
                      @"&(pound|#163);",   
                      @"&(copy|#169);",   
                      @"&#(\d+);",   
                      @"-->",   
                      @"<!--.*\n"   
                    };

        string[] aryRep =   {   
                        "",   
                        "",   
                        "",   
                        "\"",   
                        "&",   
                        "<",   
                        ">",   
                        "   ",   
                        "\xa1",//chr(161),   
                        "\xa2",//chr(162),   
                        "\xa3",//chr(163),   
                        "\xa9",//chr(169),   
                        "",   
                        "\r\n",   
                        ""   
                      };

        string newReg = aryReg[0];
        string strOutput = strHtml;
        for (int i = 0; i < aryReg.Length; i++)
        {
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(aryReg[i], System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            strOutput = regex.Replace(strOutput, aryRep[i]);
        }
        strOutput.Replace("<", "");
        strOutput.Replace(">", "");
        strOutput.Replace("\r\n", "");
        return strOutput;
    }


    //string 转float类型
    public float StrToFloat(object FloatString)
    {
        try
        {
            float f = (float)Convert.ToSingle(FloatString);
            return f;
        }
        catch (FormatException)
        {
            return (float)0.00;
        }
    }

    public static string GetParentClassName(int id)
    {
        Class c = new Class();
        string classname;
        classname = c.GetClassById(c.GetParentClassById(id)).Rows[0]["classname"].ToString();
        return classname;
    }

    /// <summary>
    /// 获取当前新闻类别下拉列表
    /// </summary>
    /// 开始
    /// 
    public string GetTree1(int id, int currentid)
    {
        k = k + 1;
        int j = 0;
        DataTable dt = new DataTable();
        SqlHelper.FillDataTable("select * from class where parentid=" + id.ToString(), dt);
        try
        {
            while (j < dt.Rows.Count)
            {
                int cid = Int32.Parse(dt.Rows[j]["classid"].ToString());
                string classname = dt.Rows[j]["classname"].ToString();
                str = str + "<option value=" + cid;
                if (cid == currentid)
                    str = str + " selected='selected' > ";
                else
                    str = str + ">";
                for (int i = 1; i <= k; i++)
                {
                    if (i != 1)
                    {
                        str = str + "├&nbsp;";
                    }
                }
                str = str + classname + "</option>";
                GetTree1(cid, currentid);
                k = k - 1;
                j++;
            }
        }
        catch
        {
            str = "";
        }

        return str;
    }

    public string ShowClass1(int type, int currentid)
    {
        return GetTree1(type, currentid);
    }
    /// <summary>
    /// 
    /// 结束
    /// </summary>
    /// 
}
